﻿using System.Collections.Generic;
using WSKnowledge.WSHelpers.DesignByContract;

namespace WSKnowledge.WSHelpers
{
	/// <summary>
	/// This helper can validates and then add items to collections.
	/// Only reference type collections are considered till now.
	/// But we can also can implement it for Value types.
	/// <example>
	///	public static bool CanAddValueType<T>(T item, ICollection<T> collection)
	///		where T : struct
	///	{
	///		return collection != null && (collection.Contains(item));
	///	}	
	/// </example>
	/// </summary>
	public static class CollectionHelper
	{
		private const string ItemIsNullErrorMessage = "Cannot add this item to collection, item equals null.";
		private const string CollectionIsNullErrorMessage = "Cannot add items to this collection, collection equals null.";

		public static void AddInstance<T>(T item, ref List<T> collection)
			where T : class
		{
			Check.Require(item != null, ItemIsNullErrorMessage);
			Check.Require(collection != null, CollectionIsNullErrorMessage);
			if (collection != null) collection.Add(item);
		}

		// Can it be "You ain't gonna need it *YAGNI* problem"
		//public static void AddCollection<T>(IEnumerable<T> items, ref List<T> collection)
		//    where T : class
		//{
		//    Check.Require(items != null, ItemIsNullErrorMessage);
		//    Check.Require(collection != null, CollectionIsNullErrorMessage);
		//    if (collection != null)
		//        if (items != null) collection.AddRange(items);
		//}

		//public static void AddInstance<T>(T item, ref ICollection<T> collection)
		//    where T : class
		//{
		//    Check.Require(item != null, ItemIsNullErrorMessage);
		//    Check.Require(collection != null, CollectionIsNullErrorMessage);
		//    if (collection != null) collection.Add(item);
		//}
	}
}
